package com.tts.dede;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.tts.dede.R;



public class MemoryView extends SurfaceView implements Runnable {

	private SurfaceHolder surfaceHolder;
	private Thread surfaceThread = null;
	private boolean isRunning = true;
	private Canvas canvas;
	private Bitmap inter;
	private ArrayList<Integer> couleurs = new ArrayList<Integer>();
	private Carte[] cartes = new Carte[12];
	private float x,y;
	private boolean touched = false;
	private int returnedCard = 0;
	private int[] returnedCardId = new int[2];
	
	public MemoryView(Context context) {
		super(context);
		surfaceHolder = getHolder();
		surfaceThread = new Thread(this);
		surfaceThread.start();

		couleurs.add(Color.BLUE);
		couleurs.add(Color.RED);
		couleurs.add(Color.YELLOW);
		couleurs.add(Color.rgb(255, 160, 0));
		couleurs.add(Color.GREEN);
		couleurs.add(Color.GRAY);
		couleurs.add(Color.BLUE);
		couleurs.add(Color.RED);
		couleurs.add(Color.YELLOW);
		couleurs.add(Color.rgb(255, 160, 0));
		couleurs.add(Color.GREEN);
		couleurs.add(Color.GRAY);
		
		for(int i=0;i<12;i++)
			cartes[i] = new Carte(getRandomColor());
	}
	
	public void pause(){
		isRunning = false;
	}
	
	public void resume(){
		isRunning = true;
		surfaceThread = new Thread(this);
		surfaceThread.start();
	}
	
	public void setX(float x){
		this.x = x;
	}
	
	public void setY(float y){
		this.y = y;
	}
	
	public void setTouched(boolean touched){
		this.touched = touched;
	}
	
	public int getRandomColor(){
		int color = 0;
		if(couleurs.size()>0){
			Random rand = new Random();
			int id = rand.nextInt(couleurs.size());
			color = couleurs.get(id);
			couleurs.remove(id);
		}
		return color;
	}
	
	
	public void run() {
		// TODO Auto-generated method stub
		while(isRunning){
			if(!surfaceHolder.getSurface().isValid())
				continue;
			
			canvas = surfaceHolder.lockCanvas();
			canvas.drawRGB(255, 255, 255);
			inter = BitmapFactory.decodeResource(getResources(), R.drawable.interrogation);
			inter = Bitmap.createScaledBitmap(inter, canvas.getWidth()/4, canvas.getHeight()/3, true);
			int width = canvas.getWidth()/4-4;
			int height = canvas.getHeight()/3-3;
			Paint paint = new Paint();
			
			int count = 0;
			for(int i=0;i<4;i++){
				for(int j=0;j<3;j++){
					if(touched && x > i*width+i*5 && x < i*width+width+i*5 && y > j*height+j*5 && y < j*height+height+j*5){
						touched = false;
						if(cartes[count].isCover() && !cartes[count].isFind() && returnedCard<2){
							cartes[count].setCover(false);
							returnedCardId[returnedCard] = count;
							returnedCard++;
							if(returnedCard == 2){
								if(cartes[returnedCardId[0]].getColor() == cartes[returnedCardId[1]].getColor()){
									cartes[returnedCardId[0]].setFind(true);
									cartes[returnedCardId[1]].setFind(true);
									returnedCard = 0;
								}else{
									new Timer().schedule(new TimerTask() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											cartes[returnedCardId[1]].setCover(true);
											cartes[returnedCardId[0]].setCover(true);
											returnedCard = 0;
										}
									}, 300);
									
								}
								
							}
						}
					}
					if(!cartes[count].isCover()){
						paint.setColor(cartes[count].getColor());
						canvas.drawRect(i*width+i*5, j*height+j*5, i*width+width+i*5, j*height+height+j*5, paint);
					}else{
						canvas.drawBitmap(inter, i*width+i*5, j*height+j*5, null);
					}
					count++;
				}
			}

			surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

}
